﻿using DalatCooking.Core;
using DalatCooking.Core.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class admin_Category_Default : System.Web.UI.Page
{
    public int total = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected override void OnInit(EventArgs e)
    {
        BinData();
        base.OnInit(e);
    }
    public string Keyword
    {
        get
        {
            if (Server.HtmlEncode(Request.QueryString["q"]) != null)
                return Server.HtmlEncode(Request.QueryString["q"]);
            else return string.Empty;
        }
    }
    void BinData()
    {

        int pageSize = 10;
        if (Request.Cookies["postPageSize"] != null)
        {
            pageSize = ConvertObject.ToInt(Server.HtmlEncode(Request.Cookies["postPageSize"].Value));
        }
        else
        {
            pageSize = 10;
        }

        int pageNum = 1;

        if (Request.QueryString[PagingControl1.QueryStringKey] != null)
        {
            pageNum = ConvertObject.ToInt(Server.HtmlEncode(Request.QueryString[PagingControl1.QueryStringKey]));
        }
        else pageNum = 1;
        if (!string.IsNullOrEmpty(Keyword))
        {
            rpt.DataSource = PagingSearch(pageSize, pageNum, out total, Keyword);
        }
        else
        {
            rpt.DataSource = CategoryManager.PagingSql(pageSize, pageNum, out total).FindAll(c => c.ParentId == 0);
        }
        rpt.DataBind();

        PagingControl1.TotalPage = total;
        PagingControl1.CurrentPage = pageNum;
        PagingControl1.PageSize = pageSize;


        PagingControl2.TotalPage = total;
        PagingControl2.CurrentPage = pageNum;
        PagingControl2.PageSize = pageSize;
    }
    public List<Category> PagingSearch(int pageSize, int pageNum, out int totalRowCount, string keywrod)
    {
        var query = from c in CategoryManager.FindAll()
                    where c.Name.Contains(keywrod)
                    select c;
        totalRowCount = query.Count();
        var list = query.Skip(pageSize * (pageNum - 1)).Take(pageSize).ToList();
        return list;
    }
    public List<Category> CategoryByParent(object parentID)
    {
        return CategoryManager.FindAll().FindAll(c => c.ParentId == ConvertObject.ToInt(parentID));
    }
    protected string GetParentTitle(object obj)
    {
        MenuGroup temp = MenuGroupManager.Single(int.Parse(obj.ToString()));
        if (temp == null)
            return "";
        else
            return temp.Name;
    }

    public int Count()
    {

        return total;
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

    }
}